#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int w[50500];
int dp[250050];
int n;
int main(void){
    while(~scanf("%d",&n)){
        if(n<0){
            break;
        }
        memset(dp,0,sizeof(dp));
        int v,m;
        int cnt=0;
        int sum=0;
        for(int i=0;i<n;i++){
            scanf("%d%d",&v,&m);
            sum+=v*m;
            while(m--){
                w[cnt++]=v;
            }
        }
        for(int i=0;i<cnt;i++){
            for(int j=sum/2;j>=w[i];j--){
                dp[j]=max(dp[j],dp[j-w[i]]+w[i]);
            }
        }
        printf("%d %d\n",max(dp[sum/2],sum-dp[sum/2]),min(dp[sum/2],sum-dp[sum/2]));
    }
    return 0;
}
